package com.hrh.selectsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrh.selectsystem.model.entity.MentorApplicationCount;
import com.hrh.selectsystem.service.MentorApplicationCountService;
import com.hrh.selectsystem.mapper.MentorApplicationCountMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author hetongxue
* @description 针对表【mentor_application_count(导师申请数表)】的数据库操作Service实现
* @createDate 2024-11-01 15:07:15
*/
@Service
public class MentorApplicationCountServiceImpl extends ServiceImpl<MentorApplicationCountMapper, MentorApplicationCount>
    implements MentorApplicationCountService{

    @Resource
    private MentorApplicationCountMapper mentorApplicationCountMapper;

    /**
     * 获取导师申请数
     * @param mentorIds
     * @return
     */
    @Override
    public Map<Long, Integer> getApplicationCountMap(List<Long> mentorIds) {
        // 根据导师id查询申请数
        List<MentorApplicationCount> counts = mentorApplicationCountMapper.selectApplicationIds(mentorIds);
        return counts.stream()
                .collect(Collectors.toMap(MentorApplicationCount::getMentorid, MentorApplicationCount::getApplicationCount));
    }

    /**
     * 更新导师申请数
     * @param MentorId
     */
    @Override
    public void updateMentorApplicationCount(Long MentorId) {
        //取出当前id导师的申请数
        MentorApplicationCount mentorApplicationCount = mentorApplicationCountMapper.selectByMentorId(MentorId);
        //如果不存在
        if (mentorApplicationCount == null) {
            //创建一个对象
            mentorApplicationCount = new MentorApplicationCount();
            //设置id
            mentorApplicationCount.setMentorid(MentorId);
            //设置申请数为1
            mentorApplicationCount.setApplicationCount(1);
            //存回数据库
            mentorApplicationCountMapper.insert(mentorApplicationCount);
            return;
        }
        Integer applicationCount = mentorApplicationCount.getApplicationCount();
        //id++
        Integer applicationCountNew = applicationCount + 1;
        //存回数据库
        MentorApplicationCount update = new MentorApplicationCount();
        update.setId(mentorApplicationCount.getId());
        update.setApplicationCount(applicationCountNew);
        mentorApplicationCountMapper.updateById(update);
    }

}




